<?php

namespace app\admin\model;
use my\Security;
use think\Db;
use think\Model;
use app\admin\model\FastCar;

class HiboShareProfitMoney extends Model {

    public function __construct() {
        parent::__construct();
    }

    //分润数据列表
    public function getListInfo($page, $limit, $data) {
        $where = [];
        //注册省份
        if(isset($data['register_province']) && $data['register_province'] != ''){
            $where['b.register_province'] = $data['register_province'];
        }
        //注册城市
        if(isset($data['register_city']) && $data['register_city'] != ''){
            $where['b.register_city'] = $data['register_city'];
        }
        //用户手机号码
        if(isset($data['user_phone']) && $data['user_phone'] != ''){
            $where['b.user_phone'] = Security::encrypt($data['user_phone']);
        }
        //分页
        $start = ($page - 1) * $limit;
        $count = Db::table('hb_share_profit_money')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->count();
        $field = 'a.*,b.user_phone,b.register_province,b.register_city';
        $list = Db::table('hb_share_profit_money')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->limit($start, $limit)->field($field)->order('id desc')->select();
        foreach ($list as $key=>$value){
            $list[$key]['add_time']     = !empty($value['add_time'])?date("Y-m-d H:i:s",$value['add_time']):'-';
            $list[$key]['update_time']  = !empty($value['update_time'])?date("Y-m-d H:i:s",$value['update_time']):'-';
            $list[$key]['money']        = sprintf('%.2f',$value['money']/100);
            $list[$key]['sum_money']    = sprintf('%.2f',$value['sum_money']/100);
            $list[$key]['user_phone']   = Security::decrypt($value['user_phone']);
            //查询用户省份城市
            $list[$key]['province']     = Db::name('region')->where(['region_id'=>$value['register_province']])->value('region_name');
            $list[$key]['city']         = Db::name('region')->where(['region_id'=>$value['register_city']])->value('region_name');
        }
        return array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list
        );
    }
    //分润流水记录
    public function getListDetail($page, $limit, $data) {
        $where = [];
        //注册省份
        if(isset($data['register_province']) && $data['register_province'] != ''){
            $where['b.register_province'] = $data['register_province'];
        }
        //注册城市
        if(isset($data['register_city']) && $data['register_city'] != ''){
            $where['b.register_city'] = $data['register_city'];
        }
        //用户手机号码
        if(isset($data['user_phone']) && $data['user_phone'] != ''){
            $where['b.user_phone'] = Security::encrypt($data['user_phone']);
        }
        //到账状态
        if(isset($data['type']) && $data['type'] != ''){
            $where['a.type'] = $data['type'];
        }
        //到账状态
        if(isset($data['recommend_type']) && $data['recommend_type'] != ''){
            $where['a.recommend_type'] = $data['recommend_type'];
        }
        //用户uid
        if(!empty($data['uid'])){
            $where['a.uid'] = $data['uid'];
        }
        //分页
        $start = ($page - 1) * $limit;
        $count = Db::table('hb_share_profit_wallet_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->count();
        $field = 'a.*,b.user_phone,b.register_province,b.register_city';
        $list = Db::table('hb_share_profit_wallet_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->limit($start, $limit)->field($field)->order('id desc')->select();
        foreach ($list as $key=>$value){
            $list[$key]['add_time']     = !empty($value['add_time'])?date("Y-m-d H:i:s",$value['add_time']):'-';
            $list[$key]['update_time']  = !empty($value['update_time'])?date("Y-m-d H:i:s",$value['update_time']):'-';
            if($value['type'] == 1){
                $list[$key]['status_name']        = "<span style='color: green'>已入账</span>";
            }elseif($value['type'] == 2){
                $list[$key]['status_name']        = "<span style='color: red'>待入账</span>";
            }
            if($value['recommend_type'] == 1){
                $list[$key]['recommend_type_name']= "直接推荐";
            }elseif($value['recommend_type'] == 2){
                $list[$key]['recommend_type_name']= "间接推荐";
            }
            $list[$key]['money']        = sprintf('%.2f',$value['money']/100);
            $list[$key]['user_phone']   = Security::decrypt($value['user_phone']);
            $list[$key]['phone']        = $value['phone'];
            //查询用户省份城市
            $list[$key]['province']     = Db::name('region')->where(['region_id'=>$value['register_province']])->value('region_name');
            $list[$key]['city']         = Db::name('region')->where(['region_id'=>$value['register_city']])->value('region_name');
        }
        return array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list
        );
    }
    //分润相关数据统计
    public function shareMoney($dateArray = []){
        //省份 - 城市搜索
        if(!empty($dateArray['province_id'])){
            $where['b.register_province'] = $dateArray['province_id'];
        }
        if(!empty($dateArray['city_id'])){
            $where['b.register_city'] = $dateArray['city_id'];
        }
        //时间搜索
        $start_time = !empty($dateArray['start_time'])?strtotime($dateArray['start_time']):strtotime(date("Y-m-d")) - 24*6*3600;
        $end_time   = !empty($dateArray['end_time'])?strtotime($dateArray['end_time']." 23:59:59"):strtotime(date("Y-m-d 23:59:59"));
        $where['b.false_status'] = 0;
        $result = [];
        //查询海宝礼包数据
        $result['noreceiveGoods']               = Db::table('xm_gift_purchase_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['a.status'=>0,'a.add_time'=>['between',[$start_time,$end_time]]])->count();                                                //未收货
        $result['receiveGoods']                 = Db::table('xm_gift_purchase_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['a.status'=>1,'a.add_time'=>['between',[$start_time,$end_time]]])->count();                                             //已收货
        //分润数据
        $result['shareMoneyAccount']            = Db::table('hb_share_profit_wallet_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['a.type'=>1,'a.false_status'=>0,'a.add_time'=>['between',[$start_time,$end_time]]])->sum('a.money');                          //分润已到账
        //echo Db::table('hb_share_profit_wallet_record')->getLastSql()."<hr/>";
        $result['shareMoneyAccountNo']          = Db::table('hb_share_profit_wallet_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['a.type'=>2,'a.false_status'=>0,'a.add_time'=>['between',[$start_time,$end_time]]])->sum('a.money');                             //分润未到账
        //echo Db::table('hb_share_profit_wallet_record')->getLastSql();die;
        //直接推荐分润 - 到账 + 未到账
        $result['shareMoneyAccountDirect']      = Db::table('hb_share_profit_wallet_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['a.type'=>1,'a.false_status'=>0,'a.recommend_type'=>1,'a.add_time'=>['between',[$start_time,$end_time]]])->sum('a.money');      //直接分润已到账
        $result['shareMoneyAccountDirectNo']    = Db::table('hb_share_profit_wallet_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['a.type'=>2,'a.false_status'=>0,'a.recommend_type'=>1,'a.add_time'=>['between',[$start_time,$end_time]]])->sum('a.money');         //直接分润未到账
        //间接推荐分润 - 到账 + 未到账
        $result['shareMoneyAccountIndirect']    = Db::table('hb_share_profit_wallet_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['a.type'=>1,'a.recommend_type'=>2,'a.false_status'=>0,'a.add_time'=>['between',[$start_time,$end_time]]])->sum('a.money');      //间接分润已到账
        $result['shareMoneyAccountIndirectNo']  = Db::table('hb_share_profit_wallet_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['a.type'=>2,'a.recommend_type'=>2,'a.false_status'=>0,'a.add_time'=>['between',[$start_time,$end_time]]])->sum('a.money');         //间接分润未到账
        //推广人数 + 分享权益人数
        $result['recommendNum']                 = Db::table('xm_user_recommend_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['a.add_time'=>['between',[$start_time,$end_time]]])->count();                                                            //推广人数
        $result['equitiesNum']                  = Db::table('xm_gift_purchase_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['a.add_time'=>['between',[$start_time,$end_time]]])->order('a.purchase_id asc')->group('a.uid')->count();       //分享权益人数
        //查询购买礼包金额
        $result['pay_money']                    = Db::table('xm_gift_purchase_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['a.add_time'=>['between',[$start_time,$end_time]]])->sum('pay_money');
        return $result;
    }
    //购买礼包数量
    public function shareMoneyDetail($dateTimeArr = [],$dateArray = []){
        //省份 - 城市搜索
        if(!empty($dateArray['province_id'])){
            $where['b.register_province'] = $dateArray['province_id'];
        }
        if(!empty($dateArray['city_id'])){
            $where['b.register_city'] = $dateArray['city_id'];
        }
        $where['b.false_status'] = 0;
        foreach ($dateTimeArr as $key=>$value){
            $start_time                 = strtotime($value);
            $end_time                   = strtotime($value . ' 23:59:59');
            $result[$value]['count']    = Db::table('xm_gift_purchase_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['a.add_time'=>['between',[$start_time,$end_time]]])->count();
        }
        return $result;
    }

    //省份+城市排行榜
    public function giftBuyNumberProvinceCity($dateArray = [],$regionType = 1,$page,$limit){
        //查询多少用户购买了礼包数量
        $start_time = !empty($dateArray['start_time'])?strtotime($dateArray['start_time']):strtotime(date("Y-m-d")) - 24*6*3600;
        $end_time   = !empty($dateArray['end_time'])?strtotime($dateArray['end_time']." 23:59:59"):strtotime(date("Y-m-d 23:59:59"));
        $where['b.false_status'] = 0;
        //查询省份
        $regionList = Db::name('region')->where(['region_level'=>$regionType])->field('region_id,region_name')->select();
        $result = [];
        foreach ($regionList as $key=>$value){
            if($regionType == 1){
                $number = Db::table('xm_gift_purchase_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['register_province'=>$value['region_id']])->where(['a.add_time'=>['between',[$start_time,$end_time]]])->count();
            }else{
                $number = Db::table('xm_gift_purchase_record')->alias('a')->join('xm_user b','a.uid = b.uid','left')->where($where)->where(['register_city'=>$value['region_id']])->where(['a.add_time'=>['between',[$start_time,$end_time]]])->count();
            }
            if(!empty($number)){
                $result[$value['region_id']]['id']      = $value['region_id'];
                $result[$value['region_id']]['name']    = $value['region_name'];
                $result[$value['region_id']]['number']  = $number;
            }
        }
        if(!empty($result)){
            $sort = array_column($result,'number');
            array_multisort($sort,SORT_DESC,$result);
        }

        $list = FastCar::arr_page($result,$page,$limit);
        if ($page && $limit) {
            return array(
                'code'  => 0,
                'msg'   => '',
                'count' => count($result),
                'data'  => $list
            );
        }else{
            return $result;
        }
    }
    //个人排行榜
    /*public function giftBuyNumberPerson(){
        $start_time = !empty($dateArray['start_time'])?strtotime($dateArray['start_time']):strtotime(date("Y-m-d")) - 24*6*3600;
        $end_time   = !empty($dateArray['end_time'])?strtotime($dateArray['end_time']." 23:59:59"):strtotime(date("Y-m-d 23:59:59"));
        $userList   = Db::table('xm_gift_purchase_record')->where(['add_time'=>['between',[$start_time,$end_time]]])->field('uid')->group('uid')->select();
        $result     = [];
        foreach ($userList as $key=>$value){
            //查询手机号
            $result[$value['uid']]['id']    = $value['uid'];
            $result[$value['uid']]['name']  = Security::decrypt(Db::name('user')->where(['uid'=>$value['uid'],'is_del'=>1,'false_status'=>0])->value('user_phone'));
            //查询购买数量
            $result[$value['uid']]['number']= Db::table('xm_gift_purchase_record')->where(['add_time'=>['between',[$start_time,$end_time]]])->where(['uid'=>$value['uid']])->count();
        }
        if(!empty($result)){
            $sort = array_column($result,'number');
            array_multisort($sort,SORT_DESC,$result);
        }
        return $result;
    }*/
}
